package com.et.book.activitys;

import com.et.book.fenghuangchi.R;
import com.et.book.views.BookView;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.SeekBar;
import android.widget.TextView;

public class EtbookActivity extends Activity {
    
	private BookView bookView = null;
	private View coverView = null;
	private TextView pageNumberView = null;
	private SeekBar pageSeek = null;
	private SeekBar.OnSeekBarChangeListener barChangeListener = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);
        bookView = (BookView)findViewById(R.id.main_bookView);
        bookView.setFocusable(true);
        bookView.requestFocus();
        bookView.currentPageNumber = 0;
        
        coverView = findViewById(R.id.coverview);
        pageNumberView = (TextView)findViewById(R.id.main_pagenumber);
        pageSeek = (SeekBar)findViewById(R.id.main_seekbar);
        barChangeListener = new SeekBar.OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar seekBar) {
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar seekBar) {
			}
			
			@Override
			public void onProgressChanged(SeekBar seekBar, int progress,
					boolean fromUser) {
				int totalPage = bookView.getToatalPage();
				float max = 100;
				pageNumberView.setText(bookView.getCurentPecent());
				bookView.gotoPage(Math.round(totalPage*progress/max));
			}
		};
        pageSeek.setOnSeekBarChangeListener(barChangeListener);
        coverView.setOnTouchListener(new View.OnTouchListener() {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (coverView.getVisibility()==View.VISIBLE) {
					coverView.setVisibility(View.GONE);
				}
				return true;
			}
		});
    }
    
    
    @Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_MENU: {
			if (coverView.getVisibility()==View.VISIBLE) {
				coverView.setVisibility(View.GONE);
			}else if (coverView.getVisibility()==View.GONE||coverView.getVisibility()==View.INVISIBLE) {
				pageSeek.setOnSeekBarChangeListener(null);
				pageSeek.setProgress(Math.round(100f*bookView.getCurrentPage()/bookView.getToatalPage()));
				pageSeek.setOnSeekBarChangeListener(barChangeListener);
				pageNumberView.setText(bookView.getCurentPecent());
				coverView.setVisibility(View.VISIBLE);
			}
			return true;
		}
		case KeyEvent.KEYCODE_BACK: {
			if (coverView.getVisibility()==View.VISIBLE) {
				coverView.setVisibility(View.GONE);
				return true;
			}
			break;
		}
		}
		
		return super.onKeyUp(keyCode, event);
	}
}